package com.zlz.m3u8download.controller;

import com.zlz.m3u8download.bean.ResultSet;
import com.zlz.m3u8download.bean.TaskCacheResult;
import com.zlz.m3u8download.service.CacheTaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author: zhulinzhong
 * @Date: 2024/12/4
 */
@RestController
@RequestMapping("/m3u8")
@CrossOrigin(origins = {"http://localhost:8080"})
public class M3U8CacheController {

    @Resource
    private CacheTaskService cacheTaskService;

    /**
     * 获取代理url
     *
     * @param url
     * @return
     */
    @GetMapping("/proxy")
    public ResultSet<TaskCacheResult> fastCache(@RequestParam String url, @RequestParam(required = false) Long userId) throws IOException {
        if (url.startsWith("http://ss.zlzblog.com")
                || url.startsWith("https://ss.zlzblog.com")) {
            return ResultSet.success(TaskCacheResult.builder().taskId("-1").url(url).build());
        }
        return ResultSet.success(cacheTaskService.fastCache(url, userId == null ? -1L : userId));
    }

    /**
     * 创建下载任务
     *
     * @return
     */
    @GetMapping("/task")
    public void createCacheTask(@RequestParam(required = false) String vid,
                                @RequestParam(required = false) String url,
                                @RequestParam(required = false) Long userId) throws IOException {
        cacheTaskService.createCacheTask(vid, url, false, userId);
    }
}
